import csv

import pickle
import os

domain  = 'devevent'
data_type = 'train'

list_result = []

# 步骤 1: 读取 CSV 文件
i = 0
csv_name = f'output/dataset_csv/{domain}-{data_type}_pkl_output-{i}.csv'
csv_exist = os.path.exists(csv_name)
while csv_exist:
    csv_data = []
    with open(csv_name, 'r') as file:
        reader = csv.DictReader(file)
        for row in reader:
            # 假设每行只有一个数据点，添加第一个元素到列表
            row['time'] = float(row['time'])
            csv_data.append(row)
        list_result.append(csv_data)
    i+=1
    csv_name = f'output/dataset_csv/{domain}-{data_type}_pkl_output-{i}.csv'
    csv_exist = os.path.exists(csv_name)
    print(f"csv {csv_name} exist: {csv_exist}")

output_dir = f'output/{domain}'
# 检查output文件夹是否存在，不存在则创建
if not os.path.exists(output_dir):
    os.makedirs(output_dir)

pkl_name = f'{output_dir}/{data_type}.pkl'
# 步骤 2: 使用 pickle 序列化数据
with open(pkl_name, 'wb') as file:
    pickle.dump(list_result, file)


print(f"{domain}-{data_type} CSV Data has been written to {pkl_name}")